package dao.admins.impl;

import dao.BaseDao;
import dao.admins.ISellerQuery;
import pojo.Seller;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class SellerQueryImpl implements ISellerQuery {
    BaseDao baseDao = new BaseDao();

    @Override
    public List<Seller> queryAllSellers() {
        List<Seller> sellers = new ArrayList<>();
        String sql = "select * from seller";
        try (ResultSet rs = baseDao.executeSelect(sql, null)) {
            while (rs.next()) {
                Seller seller = new Seller();
                setSeller(seller, rs.getLong("id"), rs.getString("sellerName"), rs.getString("sellerUser"), rs.getString("sellerPassword"), rs.getString("sellerSex"),
                        rs.getTimestamp("sellerBirthday"), rs.getString("sellerIdCard"), rs.getString("sellerEmail"), rs.getString("sellerTel"), rs.getString("sellerAddress"));
                sellers.add(seller);
            }
            baseDao.closeAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return sellers;
    }

    public int addSeller(Seller seller) {
        String sql = "insert into seller(sellerName,sellerUser,sellerPassword,sellerSex,sellerBirthday,sellerIdCard,sellerEmail,sellerTel,sellerAddress) values(?,?,?,?,?,?,?,?,?)";
        Object[] params = {seller.getSellerName(), seller.getSellerUser(), seller.getSellerPassword(), seller.getSellerSex(), seller.getSellerBirthday(),
                seller.getSellerIdCard(), seller.getSellerEmail(), seller.getSellerTel(), seller.getSellerAddress()};
        int result = baseDao.executeUpdateRows(sql, params);
        baseDao.closeAll();
        return result;
    }

    @Override
    public Seller querySellerById(long id) {
        String sql = "select * from seller where id = ?";
        Seller seller = new Seller();
        Object[] params = {id};
        try (ResultSet rs = baseDao.executeSelect(sql, params)) {
            if (rs.next()) {
                setSeller(seller, rs.getLong("id"), rs.getString("sellerName"), rs.getString("sellerUser"), rs.getString("sellerPassword"), rs.getString("sellerSex"),
                        rs.getTimestamp("sellerBirthday"), rs.getString("sellerIdCard"), rs.getString("sellerEmail"), rs.getString("sellerTel"), rs.getString("sellerAddress"));
            }
            baseDao.closeAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return seller;
    }

    @Override
    public int updateSeller(Seller seller) {
        String sql = "update seller set SellerName = ? ,sellerUser = ?, sellerPassword = ?,sellerSex = ?, sellerBirthday = ?, " +
                "sellerIdCard = ?, sellerEmail = ?, sellerTel = ?, sellerAddress = ? where id = ?";
        Object[] params = {seller.getSellerName(), seller.getSellerUser(), seller.getSellerPassword(), seller.getSellerSex(), seller.getSellerBirthday(),
                seller.getSellerIdCard(), seller.getSellerEmail(), seller.getSellerTel(), seller.getSellerAddress(), seller.getId()};
        int result = baseDao.executeUpdateRows(sql, params);
        baseDao.closeAll();
        return result;
    }

    @Override
    public int deleteSeller(long id) {
        String sql = "delete from seller where id = ?";
        Object[] params = {id};
        int result = baseDao.executeUpdateRows(sql, params);
        baseDao.closeAll();
        return result;
    }

    public static final void setSeller(Seller seller, long id, String sellerName, String sellerUser, String sellerPassword, String sellerSex, Timestamp sellerBirthday,
                           String sellerIdCard, String sellerEmail, String sellerTel, String sellerAddress) {
        seller.setId(id);
        seller.setSellerName(sellerName);
        seller.setSellerUser(sellerUser);
        seller.setSellerPassword(sellerPassword);
        seller.setSellerSex(sellerSex);
        seller.setSellerBirthday(sellerBirthday);
        seller.setSellerIdCard(sellerIdCard);
        seller.setSellerEmail(sellerEmail);
        seller.setSellerTel(sellerTel);
        seller.setSellerAddress(sellerAddress);
    }
}
